package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BdmReceiveAddressMapper;
import com.ruoyi.system.domain.BdmReceiveAddress;
import com.ruoyi.system.service.IBdmReceiveAddressService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 用户地址Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-13
 */
@Service
public class BdmReceiveAddressServiceImpl extends ServiceImpl<BdmReceiveAddressMapper,BdmReceiveAddress> implements IBdmReceiveAddressService
{
    @Autowired
    private BdmReceiveAddressMapper bdmReceiveAddressMapper;

    /**
     * 查询用户地址
     * 
     * @param id 用户地址主键
     * @return 用户地址
     */
    @Override
    public BdmReceiveAddress selectBdmReceiveAddressById(String id)
    {
        return bdmReceiveAddressMapper.selectBdmReceiveAddressById(id);
    }

    /**
     * 查询用户地址列表
     * 
     * @param bdmReceiveAddress 用户地址
     * @return 用户地址
     */
    @Override
    public List<BdmReceiveAddress> selectBdmReceiveAddressList(BdmReceiveAddress bdmReceiveAddress)
    {
        return bdmReceiveAddressMapper.selectBdmReceiveAddressList(bdmReceiveAddress);
    }

    /**
     * 新增用户地址
     * 
     * @param bdmReceiveAddress 用户地址
     * @return 结果
     */
    @Override
    public int insertBdmReceiveAddress(BdmReceiveAddress bdmReceiveAddress)
    {
        bdmReceiveAddress.setCreateTime(DateUtils.getNowDate());
        return bdmReceiveAddressMapper.insertBdmReceiveAddress(bdmReceiveAddress);
    }

    /**
     * 修改用户地址
     * 
     * @param bdmReceiveAddress 用户地址
     * @return 结果
     */
    @Override
    public int updateBdmReceiveAddress(BdmReceiveAddress bdmReceiveAddress)
    {
        bdmReceiveAddress.setUpdateTime(DateUtils.getNowDate());
        return bdmReceiveAddressMapper.updateBdmReceiveAddress(bdmReceiveAddress);
    }

    /**
     * 批量删除用户地址
     * 
     * @param ids 需要删除的用户地址主键
     * @return 结果
     */
    @Override
    public int deleteBdmReceiveAddressByIds(String[] ids)
    {
        return bdmReceiveAddressMapper.deleteBdmReceiveAddressByIds(ids);
    }

    /**
     * 删除用户地址信息
     * 
     * @param id 用户地址主键
     * @return 结果
     */
    @Override
    public int deleteBdmReceiveAddressById(String id)
    {
        return bdmReceiveAddressMapper.deleteBdmReceiveAddressById(id);
    }

    /**
     * 校验用户收获地址信息
     * @param bdmReceiveAddress
     */
    @Override
    public void checkUserAddress(BdmReceiveAddress bdmReceiveAddress) {
        if (StringUtils.isBlank(bdmReceiveAddress.getIsDefault())) {
            throw new RuntimeException("请设置是否默认地址");
        }
        if (StringUtils.isBlank(bdmReceiveAddress.getPhone())) {
            throw new RuntimeException("请输入手机号");

        }
        if (bdmReceiveAddress.getPhone().length() != 11) {
            throw new RuntimeException("手机号长度错误");
        }
        if (StringUtils.isBlank(bdmReceiveAddress.getDetail())){
            throw new RuntimeException("请输入地址详情");
        }
        String regex = "^1[3-9]\\d{9}$";
        boolean matches = bdmReceiveAddress.getPhone().matches(regex);
        if (!matches) {
            throw new RuntimeException("手机号格式错误");
        }
        if (bdmReceiveAddress.getIsDefault().equalsIgnoreCase("0")) {
            BdmReceiveAddress address = new BdmReceiveAddress();
            address.setUid(getUserId().toString());
            address.setIsDefault("1");
            this.lambdaUpdate().eq(BdmReceiveAddress::getUid, address.getUid()).set(BdmReceiveAddress::getIsDefault,"1").update();
        }
    }

    /**
     * 新增用户地址
     * @param bdmReceiveAddress
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void saveAddress(BdmReceiveAddress bdmReceiveAddress) {
        this.checkUserAddress(bdmReceiveAddress);
        bdmReceiveAddress.setUid(getUserId().toString());
        this.insertBdmReceiveAddress(bdmReceiveAddress);
    }

    /**
     * 修改用户地址
     * @param bdmReceiveAddress
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void updateAddress(BdmReceiveAddress bdmReceiveAddress) {
        this.checkUserAddress(bdmReceiveAddress);
        this.updateBdmReceiveAddress(bdmReceiveAddress);
    }
}
